﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using System.ComponentModel;

namespace AnaViewer
{
    partial class AnaForm
    {

        private void InitializeMenus()
        {
            this.ContextMenuStrip = contextMenu;
            notifyIcon.ContextMenuStrip = contextMenu;
            InitEventHandler();
        }

        private void UpdateMenuState()
        {
            menuEmbedDesktop.Checked = base.EmbedDesktop;
            menuFormLocked.Checked = this.FormLocked;
            //menuTopMost.Checked = this.TopMost;

            menuAutoStart.Checked = AutoStartHelper.IsValueExist();
        }

        void InitEventHandler()
        {
            contextMenu.Opening += delegate(object sender, CancelEventArgs e) { UpdateMenuState(); };

            menuMainForm.Click += delegate(object sender, EventArgs e) { Program.ShowMainForm(); };
            notifyIcon.DoubleClick += delegate(object sender, EventArgs e) { Program.ShowMainForm(); };
            //选项
            menuOptions.Click += delegate(object sender, EventArgs e) { ShowOptions(); };

            ////透明度
            //menuOpacity_00.Click += delegate(object sender, EventArgs e) { this.OpacityPercent = 0; };
            //menuOpacity_20.Click += delegate(object sender, EventArgs e) { this.OpacityPercent = 20; };
            //menuOpacity_50.Click += delegate(object sender, EventArgs e) { this.OpacityPercent = 50; };
            //menuOpacity_80.Click += delegate(object sender, EventArgs e) { this.OpacityPercent = 80; };
            //menuOpacity_100.Click += delegate(object sender, EventArgs e) { this.OpacityPercent = 100; };
            //menuOpacity_Custom.Click +=new EventHandler(menuOpacity_Custom_Click);
            //menuOpacityPercent.DropDownOpening += new EventHandler(menuOpacityPercent_DropDownOpening);


            menuTopMost.Click += delegate(object sender, EventArgs e)
            {
                menuTopMost.Checked = !menuTopMost.Checked;
                this.TopMost = menuTopMost.Checked;
            };

            menuEmbedDesktop.Click += delegate(object sender, EventArgs e)
            {
                menuEmbedDesktop.Checked = !menuEmbedDesktop.Checked;
                base.EmbedDesktop = menuEmbedDesktop.Checked;

                if (base.EmbedDesktop)
                    Common.ToggleDesktop();
            };
            menu鼠标穿透.Click += delegate(object sender, EventArgs e)
            {
                menu鼠标穿透.Checked = !menu鼠标穿透.Checked;
                base.CanPenetrate = menu鼠标穿透.Checked;
            };

            menuFormLocked.Click += delegate(object sender, EventArgs e)
            {
                menuFormLocked.Checked = !menuFormLocked.Checked;
                this.FormLocked = menuFormLocked.Checked;
            };

            //自动开始
            menuAutoStart.Click += delegate(object sender, EventArgs e)
            {
                menuAutoStart.Checked = !this.menuAutoStart.Checked;
                AutoStartHelper.AutoStartSwitch(menuAutoStart.Checked);
            };

            //帮助
            menuAbout.Click += delegate(object sender, EventArgs e) { Help_About(); };
            //退出
            menuExit.Click += delegate(object sender, EventArgs e) { CloseMe(); };
            //隐藏任务栏图标
            //menuHideTaskbarIcon.Click += delegate(object sender, EventArgs e) { notifyIcon.Visible = false; };

        }
        OptionsForm optionsForm;
        private void ShowOptions()
        {//因为任务栏图标的菜单有可能多次弹出设置对话框，这里要处理一下
            if (optionsForm == null || optionsForm.IsDisposed)
                optionsForm = new OptionsForm();
            if (optionsForm.Visible)
                optionsForm.Activate();
            else
                optionsForm.ShowDialog(this.TopLevelControl);
        }
        /// <summary>
        /// 帮助 - 关于
        /// </summary>
        private void Help_About()
        {
            AboutForm form = new AboutForm();
            form.ShowDialog(this.TopLevelControl);
            form.Dispose();
        }
        void menuOpacityPercent_DropDownOpening(object sender, EventArgs e)
        {
            //menuOpacity_00.Checked = false;
            //menuOpacity_20.Checked = false;
            //menuOpacity_50.Checked = false;
            //menuOpacity_80.Checked = false;
            //menuOpacity_100.Checked = false;
            //menuOpacity_Custom.Checked = false;

            //switch (this.OpacityPercent)
            //{
            //    case 0: menuOpacity_00.Checked = true; break;
            //    case 20: menuOpacity_20.Checked = true; break;
            //    case 50: menuOpacity_50.Checked = true; break;
            //    case 80: menuOpacity_80.Checked = true; break;
            //    case 100: menuOpacity_100.Checked = true; break;
            //    default: menuOpacity_Custom.Checked = true; break;
            //}
        }
        void menuOpacity_Custom_Click(object sender, EventArgs e)
        {
            //OpacityPercentForm form = new OpacityPercentForm();
            //form.AlphaForm = this;
            //int prePercent = this.OpacityPercent;
            //form.Value = this.OpacityPercent;
            //if (form.ShowDialog(this.TopLevelControl) == DialogResult.OK)
            //{
            //    this.OpacityPercent = form.Value;
            //}
            //else
            //{
            //    this.OpacityPercent = prePercent;
            //}
        }
    }
}
